package com.googlecode.contests_repo.cmd.tools;

import java.util.List;

import com.googlecode.contests_repo.validation.*;

/**
 * Command line tool for validating contest resources.
 * 
 * @author joro, petko
 * 
 */
public class ContestRepoValidator {

	private static final String SERIES = "-s";
	private static final String CONTEST = "-c";
	private static final String PROBLEM = "-p";

	/**
	 * Validates the resource file structure and prints the result. Use the -s
	 * option to validate a series, -c to validate a contest and -p to validate
	 * a problem.
	 * 
	 * @param args
	 *            args[0] = "-p | -c | -s"; args[1] = resource path
	 */
	public static void main(String[] args) {
		if (args == null || args.length != 2) {
			System.out
					.println("Usage: validateresource -option <resource path>");
			return;
		}

		String option = args[0];
		String resourcePath = args[1];
		ResourceValidator validator = createValidator(option);

		if (validator == null) {
			System.out
					.println("validateresource: invalid option (use -p, -c or -s)");
		} else {
			List<String> errors = validator.validate(resourcePath);
			if (errors == null || errors.size() < 1) {
				System.out
						.println("validateresource: the specified resource is valid");
			} else {
				for (String error : errors) {
					System.out.println(error);
				}
			}
		}
	}

	/**
	 * Creates a resource validator based on the specified option ("-s" for
	 * series, "-c" for contest, "-p" for problem)
	 * 
	 * @param option
	 * @return the resource validator instance
	 */
	public static ResourceValidator createValidator(String option) {
		ResourceValidator validator = null;
		if (SERIES.equalsIgnoreCase(option)) {
			validator = new SeriesValidator();
		} else if (CONTEST.equalsIgnoreCase(option)) {
			validator = new ContestValidator();
		} else if (PROBLEM.equalsIgnoreCase(option)) {
			validator = new ProblemValidator();
		}

		return validator;
	}
}
